<template>
  <a-modal
    :visible="visible"
    :title="t('edit')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel(false)"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
     
     <a-form-item ref="name" :label="t('name')" name="name"
        ><a-input v-model:value="formState.name" readonly/>
      </a-form-item>
      <a-form-item ref="desc" :label="t('desc')" name="desc"
        ><a-textarea v-model:value="formState.desc" />
      </a-form-item>
      <a-form-item ref="group" :label="t('group')" name="group"
        ><a-input v-model:value="formState.group" />
      </a-form-item>
      <a-form-item ref="type" :label="t('type')" name="type"
        ><a-radio-group v-model:value="formState.type" disabled>
          <a-radio :value="1">{{ t("string") }}</a-radio>
          <a-radio :value="2">{{ t("number") }}</a-radio>
          <a-radio :value="3">{{ t("string array") }}</a-radio>
          <a-radio :value="4">{{ t("number array") }}</a-radio>
          <a-radio :value="5">{{ t("string dictionary") }}</a-radio>
          <a-radio :value="6">{{ t("number dictionary") }}</a-radio>
        </a-radio-group>
      </a-form-item>
      
       <a-form-item
        v-if="formState.type == 1"
        ref="value_object"
        :label="t('value')"
        name="value_object"
        ><a-textarea v-model:value="formState.value_object" />
      </a-form-item>

      <a-form-item
        v-else-if="formState.type == 2"
        ref="value_object"
        :label="t('value')"
        name="value_object"
        ><a-input-number style="width: 100%" v-model:value="formState.value_object" />
      </a-form-item>

      <a-row v-else :gutter="8" align="top">
        <template
          v-for="(val, index) in formState.value_object"
          :key="val.index"
        >
          <template v-if="formState.type == 3 || formState.type == 4">
            <a-col :span="23">
              <a-form-item
                :label="t('value')"
                :name="['value_object', index, 'value']"
                :rules="{
                  required: true,
                  message: t('required'),
                }"
              >
                <a-input
                  v-if="formState.type == 3"
                  v-model:value="val.value"
                  :placeholder="t('string value')"
                />
                <a-input-number
                  v-else
                  style="width: 100%"
                  v-model:value="val.value"
                  :placeholder="t('number value')"
                />
              </a-form-item>
            </a-col>
            <a-col :span="1">
              <go2-icon
                @click="removeData(val)"
                type="MinusCircleOutlined"
                style="margin-top: 10px"
              />
            </a-col>
          </template>

          <template v-else-if="formState.type == 5 || formState.type == 6">
            <a-col :span="11">
              <a-form-item
                :label="t('key')"
                :name="['value_object', index, 'key']"
                :rules="{
                  required: true,
                  message: t('required'),
                }"
              >
                <a-input 
                  v-model:value="val.key"
                  :placeholder="t('key')"
                /> 
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item
                :label="t('value')"
                :name="['value_object', index, 'value']"
                :rules="{
                  required: true,
                  message: t('required'),
                }"
              >
                <a-input
                  v-if="formState.type == 5"
                  v-model:value="val.value"
                  :placeholder="t('string value')"
                />
                <a-input-number
                  v-else
                  style="width: 100%"
                  v-model:value="val.value"
                  :placeholder="t('number value')"
                />
              </a-form-item>
            </a-col>
            <a-col :span="1">
              <go2-icon
                @click="removeData(val)"
                type="MinusCircleOutlined"
                style="margin-top: 10px"
              />
            </a-col>
          </template>

        </template>

        <a-button type="dashed" block @click="addData()">
          <go2-icon type="PlusOutlined" />
          {{ t("add data") }}
        </a-button>
      </a-row>

    </a-form>
  </a-modal>
</template>
<script>
import { useI18n  } from "vue-i18n";
import { message } from 'ant-design-vue';
import { edit } from "@/core/api/module/SysConfig";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object
    }
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [],
      showComponent: true,
      showPath: true,
      showCode: false
    });
    
    const formRef = ref();

   const formState = reactive({
     
    });
 
     
    watch (() => props.record, (val) => { 
       formState.name =  props.record.name
      formState.desc = props.record.desc
      formState.group = props.record.group
      formState.type = props.record.type
      formState.value_object = JSON.parse(props.record.value_string)
      formState.value_string = ""
    });

    const addData = () => {
      formState.value_object.push({
        value: "",
      });
    };
    const removeData = (item) => {
      let index = formState.value_object.indexOf(item);
      if (index !== -1) {
        formState.value_object.splice(index, 1);
      }
    };
    
    
    const formRules = {
     name: [{ required: true, message:t('name') + t("must input"), trigger: "blur" }], 
    };
 

    const submit = (e) => {
      formRef.value
        .validate()
        .then(() => {
          formState.value_string = JSON.stringify(formState.value_object);  
          edit(toRaw(formState)).then((response)=>{
              if(response.code == 0){
                message.success({ content: '保存成功', duration: 1 }).then(()=>{
                  cancel(true);
                });
              }else{
                message.error({ content: response.msg, duration: 1 }).then(()=>{
                  cancel(false);
                });
              }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

     
    const cancel = (e=false) => {
     // formRef.value.resetFields();
      context.emit("close", e);
    };
 

    return {
      t,
      state,
      formState,
      submit, 
      cancel,
      formRef, 
      formRules, 
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      addData,
      removeData,
    };
  },
});
</script>